#ifndef DEFS_H_INCLUDED_
#define DEFS_H_INCLUDED_
-#include <algorithm> // for sort, stable_sort
-#include <cmath> // for M_PI
-#include <cstdarg> // for va_list
-#include <cstddef> // for NULL, nullptr_t, size_t
-#include <cstdint> // for int32_t, uint32_t
-#include <cstdio> // for NULL, fprintf, FILE, stdout
-#include <ctime> // for time_t
-#include <utility> // for move
+#include <algorithm> // for sort, stable_sort
+#include <cmath> // for M_PI
+#include <cstdarg> // for va_list
+#include <cstddef> // for NULL, nullptr_t, size_t
+#include <cstdint> // for int32_t, uint32_t
+#include <cstdio> // for NULL, fprintf, FILE, stdout
+#include <ctime> // for time_t
+#include <utility> // for move
#if HAVE_CONFIG_H
#include "config.h"
#endif
#if HAVE_LIBZ
-#include <zlib.h> // doesn't really belong here, but is missing elsewhere.
+#include <zlib.h> // doesn't really belong here, but is missing elsewhere.
#elif !ZLIB_INHIBITED
-#include "zlib.h" // doesn't really belong here, but is missing elsewhere.
+#include "zlib.h" // doesn't really belong here, but is missing elsewhere.
#endif
-#include <QtCore/QList> // for QList, QList<>::const_reverse_iterator, QList<>::reverse_iterator
-#include <QtCore/QString> // for QString
-#include <QtCore/QStringRef> // for QStringRef
-#include <QtCore/QTextCodec> // for QTextCodec
-#include <QtCore/QVector> // for QVector
-#include <QtCore/Qt> // for CaseInsensitive
-#include <QtCore/QtGlobal> // for foreach
+#include <QtCore/QList> // for QList, QList<>::const_reverse_iterator, QList<>::reverse_iterator
+#include <QtCore/QScopedPointer> // for QScopedPointer
+#include <QtCore/QString> // for QString
+#include <QtCore/QStringRef> // for QStringRef
+#include <QtCore/QTextCodec> // for QTextCodec
+#include <QtCore/QVector> // for QVector
+#include <QtCore/Qt> // for CaseInsensitive
+#include <QtCore/QtGlobal> // for foreach
-#include "formspec.h" // for FormatSpecificData
-#include "inifile.h" // for inifile_t
-#include "gbfile.h" // doesn't really belong here, but is missing elsewhere.
-#include "session.h" // for session_t
-#include "src/core/datetime.h" // for DateTime
-#include "src/core/optional.h" // for optional
+#include "formspec.h" // for FormatSpecificData
+#include "inifile.h" // for inifile_t
+#include "gbfile.h" // doesn't really belong here, but is missing elsewhere.
+#include "session.h" // for session_t
+#include "src/core/datetime.h" // for DateTime
+#include "src/core/optional.h" // for optional
#define CSTR(qstr) ((qstr).toUtf8().constData())
char* lrtrim(char* buff);
int xasprintf(char** strp, const char* fmt, ...) PRINTFLIKE(2, 3);
int xasprintf(QString* strp, const char* fmt, ...) PRINTFLIKE(2, 3);
+int xasprintf(QScopedPointer<char, QScopedPointerPodDeleter>& strp, const char* fmt, ...) PRINTFLIKE(2, 3);
int xvasprintf(char** strp, const char* fmt, va_list ap);
char* strupper(char* src);
char* strlower(char* src);
#include <cctype> // for isprint, toupper
#include <cmath> // for fabs, lround
-#include <cstdio> // for sprintf, sscanf, snprintf, size_t
+#include <cstdio> // for sscanf, size_t
#include <cstdlib> // for atoi, atof, strtoul
#include <cstring> // for strchr, strncmp, strlen, memmove, strrchr, memset
#include <ctime> // for gmtime
#include <QtCore/QFileInfoList> // for QFileInfoList
#include <QtCore/QLatin1String> // for QLatin1String
#include <QtCore/QList> // for QList
+#include <QtCore/QScopedPointer> // for QScopedPointer
#include <QtCore/QString> // for QString, operator==
#include <QtCore/QStringList> // for QStringList
#include <QtCore/QTime> // for QTime
#define debug_serial (global_opts.debug_level > 1)
static QString termread(char* ibuf, int size);
-static void termwrite(char* obuf, int size);
+static void termwrite(const char* obuf, int size);
static void mag_readmsg(gpsdata_type objective);
static void mag_handon();
static void mag_handoff();
{
unsigned int osum = mag_checksum(buf);
int retry_cnt = 5;
- char obuf[1000];
+ QScopedPointer<char, QScopedPointerPodDeleter> obuf;
if (debug_serial) {
warning("WRITE: $%s*%02X\r\n",buf, osum);
retry:
- int i = sprintf(obuf, "$%s*%02X\r\n",buf, osum);
- termwrite(obuf, i);
+ int i = xasprintf(obuf, "$%s*%02X\r\n",buf, osum);
+ termwrite(obuf.data(), i);
if (magrxstate == mrs_handon || magrxstate == mrs_awaiting_ack) {
magrxstate = mrs_awaiting_ack;
mag_readmsg(trkdata);
static void
mag_writeack(int osum)
{
- char obuf[200];
- char nbuf[200];
+ QScopedPointer<char, QScopedPointerPodDeleter> nbuf;
+ QScopedPointer<char, QScopedPointerPodDeleter> obuf;
if (is_file) {
return;
}
- (void) sprintf(nbuf, "PMGNCSM,%02X", osum);
- unsigned int nsum = mag_checksum(nbuf);
- int i = sprintf(obuf, "$%s*%02X\r\n",nbuf, nsum);
+ (void) xasprintf(nbuf, "PMGNCSM,%02X", osum);
+ unsigned int nsum = mag_checksum(nbuf.data());
+ int i = xasprintf(obuf, "$%s*%02X\r\n",nbuf.data(), nsum);
if (debug_serial) {
- warning("ACK WRITE: %s",obuf);
+ warning("ACK WRITE: %s",obuf.data());
}
/*
* Don't call mag_writemsg here so we don't get into ack feedback
* loops.
*/
- termwrite(obuf, i);
+ termwrite(obuf.data(), i);
}
static void
}
static void
-termwrite(char* obuf, int size)
+termwrite(const char* obuf, int size)
{
if (is_file) {
size_t nw;
void
mag_waypt_pr(const Waypoint* waypointp)
{
- char obuf[200];
- char ofmtdesc[200];
+ QScopedPointer<char, QScopedPointerPodDeleter> obuf;
+ QScopedPointer<char, QScopedPointerPodDeleter> ofmtdesc;
QString icon_token;
double ilat = waypointp->latitude;
if (global_opts.smart_icons &&
waypointp->gc_data->diff && waypointp->gc_data->terr) {
// It's a string and compactness counts, so "1.0" is OK to be "10".
- sprintf(ofmtdesc, "%ud/%ud %s", waypointp->gc_data->diff,
+ xasprintf(ofmtdesc, "%ud/%ud %s", waypointp->gc_data->diff,
waypointp->gc_data->terr, CSTRc(odesc));
- odesc = mag_cleanse(ofmtdesc);
+ odesc = mag_cleanse(ofmtdesc.data());
} else {
odesc = mag_cleanse(CSTRc(odesc));
}
* cap on the comments delivered so we leave space for it to route.
*/
if (!odesc.isEmpty() && (wptcmtcnt++ >= wptcmtcnt_max)) {
- odesc[0] = 0;
+ odesc.clear();
}
- sprintf(obuf, "PMGNWPL,%4.3f,%c,%09.3f,%c,%07.0f,M,%-.*s,%-.46s,%s",
+ xasprintf(obuf, "PMGNWPL,%4.3f,%c,%09.3f,%c,%07.0f,M,%-.*s,%-.46s,%s",
lat, ilat < 0 ? 'S' : 'N',
lon, ilon < 0 ? 'W' : 'E',
waypointp->altitude == unknown_alt ?
CSTRc(owpt),
CSTRc(odesc),
CSTR(icon_token));
- mag_writemsg(obuf);
+ mag_writemsg(obuf.data());
if (!is_file) {
if (mag_error) {
- warning("Protocol error Writing '%s'\n", obuf);
+ warning("Protocol error Writing '%s'\n", obuf.data());
}
}
}
static
void mag_track_disp(const Waypoint* waypointp)
{
- char obuf[200];
+ QScopedPointer<char, QScopedPointerPodDeleter> obuf;
int hms=0;
int fracsec=0;
int date=0;
lon = (lon_deg * 100.0 + lon);
lat = (lat_deg * 100.0 + lat);
- sprintf(obuf,"PMGNTRK,%4.3f,%c,%09.3f,%c,%05.0f,%c,%06d.%02d,A,,%06d",
+ xasprintf(obuf,"PMGNTRK,%4.3f,%c,%09.3f,%c,%05.0f,%c,%06d.%02d,A,,%06d",
lat, ilat < 0 ? 'S' : 'N',
lon, ilon < 0 ? 'W' : 'E',
waypointp->altitude == unknown_alt ?
0 : waypointp->altitude,
- 'M',hms,fracsec,date);
- mag_writemsg(obuf);
+ 'M', hms, fracsec, date);
+ mag_writemsg(obuf.data());
}
static
static void
mag_route_trl(const route_head* rte)
{
- char obuff[256];
- char buff1[64], buff2[64];
- char* pbuff;
+ QScopedPointer<char, QScopedPointerPodDeleter> obuff;
+ QString buff1;
+ QString buff2;
+ QString* pbuff;
QString icon_token;
/* count waypoints for this route */
}
if (i == 1) {
- pbuff = buff1;
+ pbuff = &buff1;
} else {
- pbuff = buff2;
+ pbuff = &buff2;
}
// Write name, icon tuple into alternating buff1/buff2 buffer.
- sprintf(pbuff, "%s,%s", CSTR(waypointp->shortname), CSTR(icon_token));
+ *pbuff = waypointp->shortname + ',' + icon_token;
if ((waypointp == rte->waypoint_list.back()) || ((i % 2) == 0)) {
- char expbuf[1024];
+ QString expbuf;
thisline++;
- expbuf[0] = 0;
if (explorist) {
- snprintf(expbuf, sizeof(expbuf), "%s,",
- CSTRc(rte->rte_name));
+ expbuf = rte->rte_name + ',';
}
- sprintf(obuff, "PMGNRTE,%d,%d,c,%d,%s%s,%s",
+ xasprintf(obuff, "PMGNRTE,%d,%d,c,%d,%s%s,%s",
numlines, thisline,
rte->rte_num ? rte->rte_num : route_out_count,
- expbuf,
- buff1, buff2);
+ CSTRc(expbuf),
+ CSTR(buff1), CSTR(buff2));
- mag_writemsg(obuff);
- buff1[0] = '\0';
- buff2[0] = '\0';
+ mag_writemsg(obuff.data());
+ buff1.clear();
+ buff2.clear();
i = 0;
}
}
*/
-#include <cctype> // for isspace, isalpha, ispunct, tolower, toupper
-#include <cerrno> // for errno
-#include <cmath> // for fabs, floor
-#include <cstdarg> // for va_list, va_end, va_start, va_copy
-#include <cstdio> // for size_t, vsnprintf, FILE, fopen, printf, sprintf, stderr, stdin, stdout
-#include <cstdint> // for uint32_t
-#include <cstdlib> // for abs, getenv, calloc, free, malloc, realloc
-#include <cstring> // for strlen, strcat, strstr, memcpy, strcmp, strcpy, strdup, strchr, strerror
-#include <ctime> // for mktime, localtime
-
-#include <QtCore/QByteArray> // for QByteArray
-#include <QtCore/QChar> // for QChar, operator<=, operator>=
-#include <QtCore/QCharRef> // for QCharRef
-#include <QtCore/QDateTime> // for QDateTime
-#include <QtCore/QFileInfo> // for QFileInfo
-#include <QtCore/QList> // for QList
-#include <QtCore/QString> // for QString
-#include <QtCore/QStringRef> // for QStringRef
-#include <QtCore/QTextCodec> // for QTextCodec
-#include <QtCore/QTextStream> // for operator<<, QTextStream, qSetFieldWidth, endl, QTextStream::AlignLeft
-#include <QtCore/QXmlStreamAttribute> // for QXmlStreamAttribute
-#include <QtCore/Qt> // for CaseInsensitive
-#include <QtCore/QTimeZone> // for QTimeZone
-#include <QtCore/QtGlobal> // for qAsConst, QAddConst<>::Type, qPrintable
+#include <algorithm> // for sort
+#include <cctype> // for isspace, isalpha, ispunct, tolower, toupper
+#include <cerrno> // for errno
+#include <cmath> // for fabs, floor
+#include <cstdarg> // for va_list, va_end, va_start, va_copy
+#include <cstdio> // for size_t, vsnprintf, FILE, fopen, printf, sprintf, stderr, stdin, stdout
+#include <cstdint> // for uint32_t
+#include <cstdlib> // for abs, getenv, calloc, free, malloc, realloc
+#include <cstring> // for strlen, strcat, strstr, memcpy, strcmp, strcpy, strdup, strchr, strerror
+#include <ctime> // for mktime, localtime
+
+#include <QtCore/QByteArray> // for QByteArray
+#include <QtCore/QChar> // for QChar, operator<=, operator>=
+#include <QtCore/QCharRef> // for QCharRef
+#include <QtCore/QDateTime> // for QDateTime
+#include <QtCore/QFileInfo> // for QFileInfo
+#include <QtCore/QList> // for QList
+#include <QtCore/QScopedPointer> // for QScopedPointer
+#include <QtCore/QString> // for QString
+#include <QtCore/QStringRef> // for QStringRef
+#include <QtCore/QTextCodec> // for QTextCodec
+#include <QtCore/QTextStream> // for operator<<, QTextStream, qSetFieldWidth, endl, QTextStream::AlignLeft
+#include <QtCore/QXmlStreamAttribute> // for QXmlStreamAttribute
+#include <QtCore/QXmlStreamAttributes> // for QXmlStreamAttributes
+#include <QtCore/Qt> // for CaseInsensitive
+#include <QtCore/QTimeZone> // for QTimeZone
+#include <QtCore/QtGlobal> // for qAsConst, QAddConst<>::Type, qPrintable
#include "defs.h"
-#include "cet.h" // for cet_utf8_to_ucs4
-#include "src/core/datetime.h" // for DateTime
-#include "src/core/logging.h" // for Warning
-#include "src/core/xmltag.h" // for xml_tag, xml_attribute, xml_findfirst, xml_findnext
+#include "cet.h" // for cet_utf8_to_ucs4
+#include "src/core/datetime.h" // for DateTime
+#include "src/core/logging.h" // for Warning
+#include "src/core/xmltag.h" // for xml_tag, xml_attribute, xml_findfirst, xml_findnext
// First test Apple's clever macro that's really a runtime test so
// that our universal binaries work right.
return res;
}
+int
+xasprintf(QScopedPointer<char, QScopedPointerPodDeleter>& strp, const char* fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ char* cstrp;
+ int res = xvasprintf(&cstrp, fmt, args);
+ strp.reset(cstrp);
+ va_end(args);
+
+ return res;
+}
+
int
xvasprintf(char** strp, const char* fmt, va_list ap)
{